package txtReade;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.*;

public class SliderLog
{

	private static File f;
	private static RandomAccessFile raf;
	public static final int chunk=200;

	public static void main(String[] args) throws IOException
	{
		int slideLenght = PrepareFile();
		
		Display display = new Display();
		Shell shell = new Shell(display);
		final Slider slider = new Slider(shell, SWT.VERTICAL|SWT.SCROLL_LINE);
		Rectangle clientArea = shell.getClientArea();
		slider.setBounds(clientArea.x + 10, clientArea.y + 10, 16, 400);
		slider.setMinimum(0);
		slider.setMaximum(slideLenght);
		slider.setSelection(slideLenght);


		

		
		final Text text = new Text(shell, SWT.MULTI);
		text.setBounds(clientArea.x + 30, clientArea.y + 10, 600, 400);
		text.setText(updateTextArea(slider.getSelection()));
		
		slider.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{

				try
				{
					text.setText(updateTextArea(slider.getSelection()));
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				int increment = slider.getIncrement();
//				int pos = slider.getSelection();
//				String txt = getText(pos);
//				text.setText("Scroll detail -> " + pos + " " + increment);

			}

			
		});
		shell.open();
		while (!shell.isDisposed())
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private static String  updateTextArea(int selection) throws IOException
	{
		raf.seek((selection)*chunk);
		String strLine;
		StringBuffer sb = new StringBuffer();	
		int readChar=0;
		while ((strLine = raf.readLine()) != null && readChar<chunk)
		{
			sb.append(strLine);
			sb.append("\n");
			readChar=readChar+strLine.length();
		}
		return sb.toString();
	}

	private static int PrepareFile() throws IOException
	{
		f = new File("c:/data/McBatch_stdout.2013-06-17_00-17-02_27141.log");	
		raf = new RandomAccessFile(f,"r");
		long rafLong= raf.length();
		int steps=(int) (rafLong/chunk);
		return steps+20;
	}
}
